package com.example.qcweb;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.dao.DBOpenHelper;
import com.example.modal.DataBaseUtil;
import com.example.modal.QcCard;
import com.example.modal.WorkingProcedure;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class DBGlobal {
    private static DBOpenHelper helper;
    private static SQLiteDatabase db;
    public static QcCard qcCard;


    public static QcCard GetCardInfo(String ID, Context context) {

        qcCard = DataBaseUtil.findCardInfo(ID, context);
        return qcCard;
    }

    public static Map<String, List<WorkingProcedure>> QCCardWorkProduce(Context context) {
        List<WorkingProcedure> array = DataBaseUtil.findWorkProcedure(MyGlobal.KeycardCode, context);

        //List<Map<String,List<WorkProcedure>>> workProduceList=new ArrayList<Map<String,List<WorkProcedure>>>();
        Map<String, List<WorkingProcedure>> producemap = new HashMap<String, List<WorkingProcedure>>();
        //按produce分类建成hash表。
        for (int i = 0; i < array.size(); i++) {
        /*if(workProduceList.size()==0)
		{
			Map<String,List<WorkProcedure>> map=new HashMap<String,List<WorkProcedure>>();
			listProcedures.add(array.get(i));
			map.put(array.get(i).getProduce(),listProcedures);
			workProduceList.add(map);
		}else{
			for (Map<String, List<WorkProcedure>>me:workProduceList)
			{
				Map<String, Object> tempmap=new HashMap<String, Object>();
				if (me.containsKey(array.get(i).getProduce())) {
					me.get(array.get(i).getProduce()).add(array.get(i));
				}else {
					me.put(array.get(i).getProduce(),listProcedures);
					workProduceList.add(me);
				}
			/*if (me.getKey()!=array.get(i).getProduce()) {
				//tempmap.put(me.getKey(), me);
				tempmap.put(array.get(i).getProduce(), array.get(i));
				workProduceList.add(tempmap);
			}else  {
				//修改workProduceList里面的键值为array.get(i).getProduce()的map里面的value；
			}*/

            //}
            //}*/

            if (producemap.containsKey(array.get(i).getProduce())) {
                producemap.get(array.get(i).getProduce()).add(array.get(i));
            } else {
                List<WorkingProcedure> listProcedures = new ArrayList<WorkingProcedure>();
                listProcedures.add(array.get(i));
                producemap.put(array.get(i).getProduce(), listProcedures);

            }
        }
        //对此hash表按key排序，再对key对应的value值即list<workproduce>按step再排序。
        TreeMap<String, List<WorkingProcedure>> treeMap = new TreeMap<String, List<WorkingProcedure>>(producemap);
        for (Map.Entry<String, List<WorkingProcedure>> mEntry : treeMap.entrySet()) {
            List<WorkingProcedure> listcontent = mEntry.getValue();
            Collections.sort(listcontent, new Comparator<WorkingProcedure>() {
                public int compare(WorkingProcedure wp0, WorkingProcedure wp1) {
                    int int0 = Integer.parseInt(wp0.getStep());
                    int int1 = Integer.parseInt(wp1.getStep());
                    int result = int0 - int1;
                    return result;

                }
            });
        }

        return treeMap;

    }

}
